package com.yixun.lbx.controller.farm;

import com.yixun.controller.BaseController;
import com.yixun.lbx.entity.YxProBreedFeed;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.service.BreedFeedService;
import com.yixun.lbx.service.CompanyService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/*
 * 饲喂记录 Controller
 * */
@RestController
@RequestMapping("/bf")
public class BreedFeedController extends BaseController {
    private final BreedFeedService breedFeedService;
    private final CompanyService companyService;

    public BreedFeedController(CompanyService companyService, BreedFeedService breedFeedService) {
        this.companyService = companyService;
        this.breedFeedService = breedFeedService;
    }

    /*
     *获取饲喂记录列表
     */
    @PostMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        Integer count = breedFeedService.getCount(map);
        List<YxProBreedFeed> breedFeeds = breedFeedService.getListByPage(map);

        map.put("count", count);
        map.put("breedFeeds", breedFeeds);
        return map;
    }

    /*
     *添加饲喂记录
     */
    @PostMapping("/addBreedFeed")
    public int addBreedFeed(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        map.put("addUser", this.getSessionUser().getUser_id());
        return breedFeedService.addBreedFeed(map);
    }

    /*
     *修改饲喂记录
     */
    @PostMapping("/updateBreedFeed")
    public int updateBreedFeed(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        return breedFeedService.updateBreedFeed(map);
    }

    /*
     *删除饲喂记录
     */
    @PostMapping("/deleteBreedFeed")
    public int deleteBreedFeed(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] ids = map.get("id").toString().split(",");
        map.put("ids", ids);
        return breedFeedService.deleteBreedFeed(map);
    }

    @GetMapping("/getObjByArgs")
    public List<YxProBreedFeed> getObjByArgs(YxProBreedFeed breedFeed) {
        return breedFeedService.getObjByArgs(breedFeed);
    }

    /*搜索*/
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", String.valueOf(qyId));
        }
        List<YxProBreedFeed> objs = breedFeedService.getSearchListByPage(map);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("obj", objs);
        hashMap.put("count", objs.size());
        return hashMap;
    }

    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
}
